//Name: Sara Thorup
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

void test(int handPos, int trashFlag);
void setHandDeckDiscard(struct gameState* gameState, int testPlayer);

/*
Unit tests for  int discardCard(int handPos, int currentPlayer, struct gameState *state, int trashFlag)
discardCard() removes a card for a players hand and puts them in played pile or discardpile

Things to test:
- If trashFlag = 1, then put the card in the played card (trash) pile and increment count
- If trashFlag = 0, then put the card in the discardPile and increment count
- remove from hand via -1 and decrement hand count
- -1 should be the only card or last card
*/

int main(int argc, char *argv[]) {
	printf("\n ===== Testing discardCard() =====\n");
	test(0, 0);
	test(1, 0);
	test(2,0);
	test(0, 1);
	test(1, 1);
	test(2,1);
	return 0;

}

void test(int handPos, int trashFlag) {
	
	printf("\nhand position = %d and trashflag = %d \n", handPos, trashFlag);

	//setup
	struct gameState* gameState = newGame();
	int testPlayer = 0;
	int numPlayers = 2;
	int kingdomCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
	int randomSeed = 10;
	if (initializeGame(numPlayers, kingdomCards, randomSeed, gameState) != 0){
		printf("Error initalizing game. \n");
		return;
	}
	setHandDeckDiscard(gameState, testPlayer);
	int initalDiscardCount = gameState->discardCount[testPlayer];
	int initalPlayedCount = gameState->playedCardCount;
	gameState->hand[testPlayer][handPos] = silver;
	int cardToDiscard = gameState->hand[testPlayer][handPos];
 

	discardCard(handPos, testPlayer, gameState, trashFlag);

	int expectedHandCount = 3 - 1;
	if (gameState->handCount[testPlayer] == expectedHandCount) {
		printf("discardCard(): PASS decrementing handcount \n");
	} else {
		printf("discardCard(): FAIL decrementing handcount \n");
	}


	if(gameState->hand[testPlayer][gameState->handCount[testPlayer]] == -1) {
		printf("discardCard(): PASS removing card from hand \n");
	} else {
		printf("discardCard(): FAIL removing card from hand \n");
	}

	if(trashFlag >= 1){
		if (gameState->playedCardCount == initalPlayedCount + 1){
			printf("discardCard(): PASS incrementing playedCardCount \n");
		} else {
			printf("discardCard(): FAIL incrementing playedCardCount \n");
		}

		if (gameState->playedCards[initalPlayedCount] == cardToDiscard) {
			printf("discardCard(): PASS adding card to played pile \n");
		} else {
			printf("discardCard(): FAIL adding card to played pile \n");
		}
	} else {
		if (gameState->discardCount[testPlayer] == initalDiscardCount + 1){
			printf("discardCard(): PASS incrementing discard count \n");
		} else {
			printf("discardCard(): FAIL incrementing discard count \n");
		}

		if (gameState->discard[testPlayer][initalDiscardCount] == cardToDiscard) {
			printf("discardCard(): PASS adding card to discard pile \n");
		} else {
			printf("discardCard(): FAIL adding card to discard pile \n");
		}
	}
}

void setHandDeckDiscard(struct gameState* gameState, int testPlayer) {
	gameState->handCount[testPlayer] = 3;
    gameState->discardCount[testPlayer] = 4;
    gameState->deckCount[testPlayer] = 5;
    gameState->playedCardCount = 5;

    int i = 0;
    for (; i < gameState->playedCardCount; i++) {
    	gameState->playedCards[i] = gold;
    }


    for (i = 0; i < gameState->handCount[testPlayer]; i++) {
    	gameState->hand[testPlayer][i] = gold;
    }
   
   	for (i = 0; i < gameState->discardCount[testPlayer]; i++) {
   		gameState->discard[testPlayer][i] = gold;
   	}

   	for (i = 0; i < gameState->deckCount[testPlayer]; i++) {
   		gameState->deck[testPlayer][i] = gold;
   	}

}